<template>
  <el-dialog id="edit-dialog" :title="formValidate.id ? $t('channel.btn.edit') : $t('channel.btn.add')" :visible.sync="showState" :destroy-on-close="true" :before-close="handleClose" :close-on-click-modal="false" width="650px">
    <el-form ref="formValidate" :model="formValidate" :rules="rules" label-width="150px" class="demo-ruleForm">
      <el-form-item :label="$t('channel.tabData.name')" prop="name">
        <el-input v-model="formValidate.name" :placeholder="$t('channel.prompt.name')" :disabled="!!formValidate.id" :maxlength="30" />
      </el-form-item>
      <el-form-item :label="$t('channel.tabData.companyName')" prop="companyName">
        <el-input v-model="formValidate.companyName" :placeholder="$t('channel.prompt.companyName')" :disabled="!!formValidate.id" :maxlength="30" />
      </el-form-item>
      <el-form-item :label="$t('channel.tabData.packageName')" prop="packageName">
        <el-select v-model="formValidate.packageName" :placeholder="$t('channel.prompt.packageName')" :disabled="!!formValidate.id" style="width: 100%">
          <el-option
            v-for="item in packageData"
            :key="item.packageName"
            :label="item.packageName"
            :value="item.packageName"
          />
        </el-select>
      </el-form-item>
      <el-form-item :label="$t('channel.tabData.method')" prop="clearingWay">
        <el-select v-model="formValidate.clearingWay" :placeholder="$t('channel.prompt.method')" :disabled="!!formValidate.id" style="width: 100%">
          <el-option
            v-for="item in $t('channel.method')"
            :key="item.value"
            :label="item.desc"
            :value="item.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item v-if="formValidate.clearingWay !== 'NONE'" :label="$t('channel.tabData.costSetting')" prop="everyAmount">
        <el-input-number
          v-model="formValidate.everyAmount"
          :controls="false"
          :max="10000000000"
          :placeholder="$t('channel.prompt.everyAmount')"
          :disabled="!!formValidate.id"
        />
        <el-select v-model="formValidate.everyAmountUnit" :disabled="!!formValidate.id">
          <el-option
            v-for="item in $t('channel.unit')"
            :key="item.value"
            :label="item.desc"
            :value="item.value"
          />
        </el-select>
        {{ $t('channel.everyPen') }}
      </el-form-item>
      <el-form-item :label="$t('channel.tabData.channelStatus')" prop="state">
        <el-select v-model="formValidate.state" style="width: 100%">
          <el-option
            v-for="item in $t('channel.channelStatus')"
            :key="item.value"
            :label="item.desc"
            :value="item.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item :label="$t('channel.tabData.templateUrl')" prop="templateUrl">
        <el-row>
          <el-col v-for="(item, index) in templateData" :key="index" :span="9">
            <el-image
              :style="{borderColor: item.url ===formValidate.templateUrl ? '#409EFF' : '#cccccc'}"
              :src="item.image"
              @click="selectImg(item.url)"
            />
          </el-col>
        </el-row>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="handleClose">{{ $t('common.operate.cancel') }}</el-button>
      <el-button type="primary" :loading="submitLoading" @click="handleSubmit">{{ $t('common.operate.sure') }}</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { addChannelH5, updateChannelH5 } from '@/api/operation'
export default {
  name: 'ChannelH5Dialog',
  props: {
    showDialog: {
      type: Boolean,
      default: false
    },
    chooseItem: {
      type: Object,
      default: function() {
        return {}
      }
    },
    packageData: {
      type: Array,
      default: function() {
        return []
      }
    }
  },
  data() {
    const baseUrl = process.env.VUE_APP_BASE_API
    const validateAmount = (rule, value, callback) => {
      if (this.formValidate.everyAmount && this.formValidate.everyAmountUnit !== null) {
        callback()
      } else {
        callback(new Error(this.$t('channel.tabData.costSetting') + this.$t('common.null')))
      }
    }
    return {
      submitLoading: false,
      formValidate: {
        id: null,
        name: null,
        clearingWay: null,
        packageName: null,
        companyName: null,
        everyAmount: null,
        everyAmountUnit: null,
        state: null,
        templateUrl: null,
        link: null
      },
      templateData: [
        {
          image: 'https://ina-loan-photo.oss-ap-southeast-5.aliyuncs.com/prepro/other/share1.png',
          url: baseUrl + '/#/first-edition'
        },
        {
          image: 'https://ina-loan-photo.oss-ap-southeast-5.aliyuncs.com/prepro/other/share2.png',
          url: baseUrl + '/#/second-edition'
        }
      ],
      rules: {
        name: [{ required: true, message: this.$t('channel.tabData.name') + this.$t('common.null'), trigger: 'blur' }],
        companyName: [{ required: true, message: this.$t('channel.tabData.companyName') + this.$t('common.null'), trigger: 'blur' }],
        packageName: [{ required: true, message: this.$t('channel.tabData.packageName') + this.$t('common.null'), trigger: 'blur' }],
        clearingWay: [{ required: true, message: this.$t('channel.tabData.method') + this.$t('common.null'), trigger: 'blur' }],
        everyAmount: [{ required: true, validator: validateAmount, trigger: 'blur' }],
        state: [{ required: true, message: this.$t('channel.tabData.channelStatus') + this.$t('common.null'), trigger: 'blur' }],
        templateUrl: [{ required: true, message: this.$t('channel.tabData.templateUrl') + this.$t('common.null'), trigger: 'blur' }]
      }
    }
  },
  computed: {
    showState: {
      get: function() {
        return this.showDialog
      },
      set: function() {
      }
    }
  },
  mounted() {
    this.formValidate = this.chooseItem
  },
  methods: {
    selectImg(url) {
      this.formValidate.templateUrl = url
    },
    handleClose() {
      this.$emit('change-dialog', false)
      this.$refs['formValidate'].clearValidate()
    },
    handleSubmit() {
      this.$refs['formValidate'].validate((valid) => {
        if (valid) {
          this.submitLoading = true
          const data = this.formValidate
          data.link = this.formValidate.templateUrl
          if (data.id) {
            updateChannelH5(data)
              .then(res => {
                this.submitLoading = false
                if (res.success) {
                  this.$message.success(this.$t('common.tip.editSuccess'))
                  this.$emit('edit-success')
                  this.handleClose()
                } else {
                  this.$message.error(res.message)
                }
              })
              .catch(err => {
                this.$message.error(err.message)
                this.submitLoading = false
              })
          } else {
            addChannelH5(data)
              .then(res => {
                this.submitLoading = false
                if (res.success) {
                  this.$message.success(this.$t('common.tip.addSuccess'))
                  this.$emit('edit-success')
                  this.handleClose()
                } else {
                  this.$message.error(res.message)
                }
              })
              .catch(err => {
                this.$message.error(err.message)
                this.submitLoading = false
              })
          }
        }
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.el-input-number{
  margin-right: 10px;
}
.el-col{
  text-align: center;
  .el-image{
    border: 2px solid #cccccc;
    padding: 5px;
    width: 145px;
    height: 285px;
  }
}
</style>
